#!/bin/bash

# 蒲公英配置
# 颜色输出
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
NC='\033[0m'

# 选择平台
echo -e "${YELLOW}请选择要打包的平台:${NC}"
echo "1) Android"

echo "2) iOS"
echo "3) Android 和 iOS"
read -p "请输入选项 (1/2/3): " platform_choice

# 选择环境
echo -e "${YELLOW}请选择环境:${NC}"
echo "1) 测试环境"
echo "2) 正式环境"
read -p "请输入选项 (1/2): " env_choice

# 输入更新描述
read -p "请输入更新描述: " update_description

# 根据选择设置环境变量
if [ "$env_choice" = "1" ]; then
    env="release"
    env_desc="测试环境"
    PGYER_API_KEY="affccb92d7785c8b02d39fc2e3f53eba"
    android_app_key="ebaa2a51241da4d276383085609c71b9"
    ios_app_key="485fbc860886997a752631176b649bc3"
else
    env="release"
    env_desc="正式环境"
    PGYER_API_KEY="266e3a34509c6a43dbbd6c6c537f76b7"
    android_app_key="2fd8d757111b3f1f5dd762513a17af71"
    ios_app_key="062c79ae3437d06a3342037e52947ff0"
fi
export https_proxy=http://127.0.0.1:7890 http_proxy=http://127.0.0.1:7890 all_proxy=socks5://127.0.0.1:7890
flutter clean
flutter pub get

# 开始打包
echo -e "${GREEN}开始打包 ${env_desc}...${NC}"

if [ "$platform_choice" = "1" ] || [ "$platform_choice" = "3" ]; then
    # Android 打包
    PGYER_APP_KEY="$android_app_key"
    flutter build apk --$env
    app_path="build/app/outputs/flutter-apk/app-${env}.apk"
    echo -e "${GREEN}Android打包完成${NC}"
    # 检查文件是否存在
    if [ ! -f "$app_path" ]; then
        echo -e "${RED}打包失败: 未找到Android安装包${NC}"
        exit 1
    fi
    # 上传到蒲公英
    echo -e "${GREEN}开始上传Android到蒲公英...${NC}"
    curl -F "file=@${app_path}" \
         -F "_api_key=${PGYER_API_KEY}" \
         -F "buildInstallType=2" \
         -F "buildPassword=123456" \
         -F "buildUpdateDescription=${env_desc}${update_description}" \
         https://www.pgyer.com/apiv2/app/upload
    if [ $? -eq 0 ]; then
        echo -e "${GREEN}Android上传成功!${NC}"
    else
        echo -e "${RED}Android上传失败!${NC}"
        exit 1
    fi
fi

if [ "$platform_choice" = "2" ] || [ "$platform_choice" = "3" ]; then
    # 删除旧的打包文件夹
    echo -e "${GREEN}删除旧的打包文件夹...${NC}"
    rm -rf build/ios/archive
    rm -rf build/ios/ipa
    # iOS 打包
    cd ios
    pod install
    cd ..
    PGYER_APP_KEY="$ios_app_key"
    
    # 使用 --export-options-plist 参数指定导出配置
    echo -e "${GREEN}开始为iOS创建导出配置文件...${NC}"
    cat > ios/exportOptions.plist << EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>destination</key>
	<string>export</string>
	<key>method</key>
	<string>release-testing</string>
	<key>signingStyle</key>
	<string>automatic</string>
	<key>stripSwiftSymbols</key>
	<true/>
	<key>teamID</key>
	<string>UJGMCRXMTA</string>
	<key>thinning</key>
	<string>&lt;none&gt;</string>
</dict>
</plist>

EOF
    
    # 使用导出配置进行打包
    flutter build ipa --$env --export-options-plist=ios/exportOptions.plist
    
    app_path="build/ios/ipa/flutter_syt.ipa"
    echo -e "${GREEN}iOS打包完成${NC}"
    # 检查文件是否存在
    if [ ! -f "$app_path" ]; then
        echo -e "${RED}打包失败: 未找到iOS安装包${NC}"
        exit 1
    fi
    # 上传到蒲公英
    echo -e "${GREEN}开始上传iOS到蒲公英...${NC}"
    curl -F "file=@${app_path}" \
         -F "_api_key=${PGYER_API_KEY}" \
         -F "buildInstallType=2" \
         -F "buildPassword=123456" \
         -F "buildUpdateDescription=${env_desc}${update_description}" \
         https://www.pgyer.com/apiv2/app/upload
    if [ $? -eq 0 ]; then
        echo -e "${GREEN}iOS上传成功!${NC}"
    else
        echo -e "${RED}iOS上传失败!${NC}"
        exit 1
    fi
fi

#<?xml version="1.0" encoding="UTF-8"?>
#<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
#<plist version="1.0">
#<dict>
#    <key>method</key>
#    <string>ad-hoc</string> <!-- 也可以是 ad-hoc, development, enterprise -->
#    <key>teamID</key>
#    <string>UJGMCRXMTA</string>
#    <key>signingCertificate</key>
#    <string>Apple Distribution</string>
#    <key>provisioningProfiles</key>
#    <dict>
#        <key>com.cdinternationalcompany.syhapp</key>
#        <string>syt</string>
#    </dict>
#</dict>
#</plist>